import tkinter        #导入Tkinter模块
import re
import tkinter.messagebox
from tkinter import ttk
from tkinter import *

def check():
    a1=entryIP1.get()
    a2=entryIP2.get()
    a3=entryIP3.get()
    a4=entryIP4.get()
    a5=entryIP5.get()
    a6=entryIP6.get()
    a7=entryIP7.get()
    a8=entryIP8.get()
    try:
        a1==int(a1)
    except:
        tkinter.messagebox.showerror('提示','输入源IP有误！！')
    else:
        try:
            a2==int(a2)
        except:
            tkinter.messagebox.showerror('提示','输入源IP有误！！')
        else:
            try:
                a3==int(a3)
            except:
                tkinter.messagebox.showerror('提示','输入源IP有误！！')
            else:
                try:
                    a4==int(a4)
                except:
                    tkinter.messagebox.showerror('提示','输入源IP有误！！')
                else:
                    try:
                        a5==int(a5)
                    except:
                        tkinter.messagebox.showerror('提示','输入目的IP有误！！')
                    else:
                        try:
                            a6==int(a6)
                        except:
                            tkinter.messagebox.showerror('提示','输入目的IP有误！！')
                        else:
                            try:
                                a7==int(a7)
                            except:
                                tkinter.messagebox.showerror('提示','输入目的IP有误！！')
                            else:
                                try:
                                    a8==int(a8)
                                except:
                                    tkinter.messagebox.showerror('提示','输入目的IP有误！！')
                                else:
                                    a1=int(float(a1))
                                    a2=int(float(a2))
                                    a3=int(float(a3))
                                    a4=int(float(a4))
                                    a5=int(float(a5))
                                    a6=int(float(a6))
                                    a7=int(float(a7))
                                    a8=int(float(a8))
                                    if a1>=1 and a1<=255 and a2>=0 and a2<=255 and a3>=0 and a3<=255 and a4>=0 and a4<=255 and a5>=1 and a5<=255 and a6>=0 and a6<=255 and a7>=0 and a7<=255 and a8>=0 and a8<=255:
                                        tkinter.messagebox.showinfo(title='提示',message= 'OK')
                                    else:
                                        tkinter.messagebox.showerror('提示', message= 'ip每段应在1—255之间！！')


def checksum():
    x1 = '{:0>4}'.format(bin(int(varIPa.get()))[2:])
    x2 = '{:0>4}'.format(bin(int(entryIPb.get()))[2:])
    x3 = '{:0>8}'.format(bin(int(value1.get(),2))[2:])
    x4 = '{:0>16}'.format(bin(int(entryIPc.get()))[2:])
    x5 = '{:0>16}'.format(bin(int(entryIPd.get()))[2:])
    x6 = '{:0>3}'.format(bin(int(entryIPe.get()))[2:])
    x7 = '{:0>13}'.format(bin(int(entryIPf.get()))[2:])
    x8 = '{:0>8}'.format(bin(int(entryIPg.get()))[2:])
    x9 = '{:0>8}'.format(bin(int(re.sub("\D", "", value2.get())))[2:])
    x10 = '{:0>8}'.format(bin(int(entryIP1.get()))[2:])
    x11 = '{:0>8}'.format(bin(int(entryIP2.get()))[2:])
    x12 = '{:0>8}'.format(bin(int(entryIP3.get()))[2:])
    x13 = '{:0>8}'.format(bin(int(entryIP4.get()))[2:])
    x14 = '{:0>8}'.format(bin(int(entryIP5.get()))[2:])
    x15 = '{:0>8}'.format(bin(int(entryIP6.get()))[2:])
    x16 = '{:0>8}'.format(bin(int(entryIP7.get()))[2:])
    x17 = '{:0>8}'.format(bin(int(entryIP8.get()))[2:])
    b1 = int((x1 + x2 + x3), 2)
    b2 = int(x4, 2)
    b3 = int(x5, 2)
    b4 = int((x6 + x7), 2)
    b5 = int((x8 + x9), 2)
    b6 = int((x10 + x11), 2)
    b7 = int((x12 + x13), 2)
    b8 = int((x14 + x15), 2)
    b9 = int((x16 + x17), 2)
    a = [b1, b2, b3, b4, b5, b6, b7, b8, b9]
    l = 0
    for i in a:
        l = l + i
        if l > 65535:
            l = int(l % 65536) + int(l / 65536)
    op = '{:0>16}'.format(bin(l)[2:])
    op1 = ''
    for i in op:
        if i == '1':
            i = '0'
            op1 = op1 + i
        elif i == '0':
            i = '1'
            op1 = op1 + i
    print(op1)
    op1 = ('{:0>4}'.format(hex(int(op1, 2))[2:])).upper()
    print(op1)
    varCout = StringVar(value = op1)
    entryCout = Entry(root, width=120, textvariable = varCout)
    entryCout.place(x=310, y=330, width=80, height=30)
    tkinter.messagebox.showinfo('HeaderCheckSum','HeaderCheckSum:'+ op1)



def DataCheck(Info):
    Data=Info.split(" ")
    sum=0
    for i in Data:
        sum=int('0x'+i,16)+sum
    check=0xffff-((0x0000ffff&sum)+(sum>>16))
    return check


def cancel():      #清空用户输入IP
    varIPa.set('4')
    varIPb.set('')
    varIPc.set('')
    varIPd.set('')
    varIPe.set('')
    varIPf.set('')
    varIPg.set('')
    varsIP1.set('')
    varsIP2.set('')
    varsIP3.set('')  
    varsIP4.set('')  
    varaIP1.set('')  
    varaIP2.set('')  
    varaIP3.set('')  
    varaIP4.set('')  
    varCout.set('') 

root=tkinter.Tk()
root.title("ip包首部校验")
root.geometry('500x600')
lab = tkinter.Label(root,text='输入正确的ip', font=60, fg='red',bg='yellow', relief='groove', bd=10)
lab.place(x=180, y=10, heigh=100, width=150, height=60) 

varIPa=tkinter. StringVar (value='4')
varIPb=tkinter. StringVar (value='')
varIPc=tkinter. StringVar (value='')
varIPd=tkinter. StringVar (value='')
varIPe=tkinter. StringVar (value='')
varIPf=tkinter. StringVar (value='')
varIPg=tkinter. StringVar (value='')
varsIP1=tkinter. StringVar (value='')
varsIP2=tkinter. StringVar (value='')
varsIP3=tkinter. StringVar (value='')
varsIP4=tkinter. StringVar (value='')
varaIP1=tkinter. StringVar (value='')
varaIP2=tkinter. StringVar (value='')
varaIP3=tkinter. StringVar (value='')
varaIP4=tkinter. StringVar (value='')
value1=tkinter. StringVar (value='00000000')
value2=tkinter. StringVar (value='IP(4)')
varCout = StringVar(value = '')

labelIPa=tkinter.Label(root, text='IP版本:', font=40)
labelIPa.place(x=80, y=80, width=80, height=30)
combobox1=ttk.Combobox(root, textvariable = varIPa)
combobox1['value']=('4','6')
combobox1.place(x=170, y=80, width=80, height=30)

labelIPb=tkinter.Label(root, text='IP包首部长度:', font=40)
labelIPb.place(x=43, y=130, width=105, height=30)
entryIPb=tkinter .Entry(root,textvariable= varIPb)
entryIPb.place(x=170, y=130, width=80, height=30)

labelIPc=tkinter.Label(root, text='IP包总长度:', font=40)
labelIPc.place(x=52, y=180, width=105, height=30)
entryIPc=tkinter .Entry(root,textvariable= varIPc)
entryIPc.place(x=170, y=180, width=80, height=30)

labelIPd=tkinter.Label(root, text='标识字段:', font=40)
labelIPd.place(x=72, y=230, width=80, height=30)
entryIPd=tkinter .Entry(root,textvariable= varIPd)
entryIPd.place(x=170, y=230, width=80, height=30)

labelIPe=tkinter.Label(root, text='标志字段:', font=40)
labelIPe.place(x=72, y=280, width=80, height=30)
entryIPe=tkinter .Entry(root,textvariable= varIPe)
entryIPe.place(x=170, y=280, width=80, height=30)

labelIPf=tkinter.Label(root, text='偏移量:', font=40)
labelIPf.place(x=80, y=330, width=80, height=30)
entryIPf=tkinter .Entry(root,textvariable= varIPf)
entryIPf.place(x=170, y=330, width=80, height=30)

labelIPg=tkinter.Label(root, text='生存时间:', font=40)
labelIPg.place(x=262, y=80, width=80, height=30)
entryIPg=tkinter .Entry(root,textvariable= varIPg)
entryIPg.place(x=360, y=80, width=80, height=30)

labelIPh=tkinter.Label(root, text='选择服务类型:', font=40)
labelIPh.place(x=300, y=140, width=110, height=30)
combobox2=ttk.Combobox(root, textvariable = value1)
combobox2['value']=('00000000','00100000','01000000','01100000','10000000','10100000','11000000','11100000')
combobox2.place(x=300,y=170,width=100,height=30)

labelIPg=tkinter.Label(root, text='选择网络协议:', font=40)
labelIPg.place(x=300, y=220, width=110, height=30)
combobox3=ttk.Combobox(root, textvariable = value2)
combobox3['value']=('ICMP(1)', 'IGMP(2)', 'GGP(3)', 'IP(4)','TCP(6)','EGP(8)','UDP(17)','IGRP(88)','OSPF(89)')
combobox3.place(x=300,y=250,width=100,height=30)

label=tkinter.Label(root, text='ip校验和:', font=40)
label.place(x=310, y=300, width=80, height=30)
entryCout = Entry(root, width=120, textvariable = varCout)
entryCout.place(x=310, y=330, width=80, height=30)


labelIP1=tkinter.Label(root, text='源IP:', font=40, justify='right')#创建标签
labelIP1.place(x=88, y=400, width=80, height=30)
labelIP2=tkinter.Label(root, text='目的IP:', font=40, justify='right')
labelIP2.place(x=80, y=450, width=80, height=30)

#源IP文本框
entryIP1=tkinter .Entry(root,textvariable= varsIP1)    #创建文本框,同时设置关联的变量
entryIP1.place(x=180, y=400, width=40, height=30)
entryIP2=tkinter .Entry(root,textvariable= varsIP2)
entryIP2.place(x=240, y=400, width=40, height=30)
entryIP3=tkinter .Entry(root,textvariable= varsIP3)
entryIP3.place(x=300, y=400, width=40, height=30)
entryIP4=tkinter .Entry(root,textvariable= varsIP4)
entryIP4.place(x=360, y=400, width=40, height=30)
#目的IP文本框
entryIP5=tkinter .Entry(root,textvariable= varaIP1)    #创建文本框,同时设置关联的变量
entryIP5.place(x=180, y=450, width=40, height=30)
entryIP6=tkinter .Entry(root,textvariable= varaIP2)
entryIP6.place(x=240, y=450, width=40, height=30)
entryIP7=tkinter .Entry(root,textvariable= varaIP3)
entryIP7.place(x=300, y=450, width=40, height=30)
entryIP8=tkinter .Entry(root,textvariable= varaIP4)
entryIP8.place(x=360, y=450, width=40, height=30)


buttonCheck=tkinter. Button(root, text='检验', font=40,activeforeground='red',
                        	activebackground='yellow', command=check)    #创建按钮组件，同时设置按钮事件处理函数,command= ok
buttonCheck. place(x=100, y=500, width=70, height=50)
buttonOk=tkinter. Button(root, text='计算', font=40,activeforeground='red',
                        	activebackground='yellow', command=checksum)    #创建按钮组件，同时设置按钮事件处理函数,command= ok
buttonOk. place(x=215, y=500, width=70, height=50)
buttonCancel=tkinter.Button(root, text='清除', font=40,activeforeground='red',
                        	activebackground='yellow', command=cancel)
buttonCancel.place(x=330, y=500, width=70, height=50)
root. mainloop()






